package com.neusoft.yuejian_01.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.AttentionBean;
import com.neusoft.yuejian_01.javabean.IsAttentionBean;
import com.neusoft.yuejian_01.javabean.IsMatchReg;
import com.neusoft.yuejian_01.javabean.MatchBean;
import com.neusoft.yuejian_01.javabean.NoRegBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.AttentionModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;
import com.neusoft.yuejian_01.model.MatchModel;

import it.sephiroth.android.library.picasso.Picasso;

public class MatchDetailActivity extends BaseActivity implements RetrofitListener {
    private TextView matchaddressView;
    private TextView matchtimeView;
    private TextView matchphoneView;
    private TextView matchpriceView;
    private TextView matchinfoView;
    private TextView matchtitleView;
    protected Context context;
    private Button regButton;//立即报名按钮
    private ImageButton button_back, matchButton, callButton;//返回按钮、关注按钮、拨打电话按钮
    public ImageView matchImageView;
    private int m_id;
    private boolean flags;
    private boolean flagr;

     protected void initView() {
         setContentView(R.layout.activity_match_detail);
        matchtitleView = (TextView) findViewById(R.id.title_match);
        matchaddressView = (TextView) findViewById(R.id.match_address);
        matchtimeView = (TextView) findViewById(R.id.match_time);
        matchphoneView = (TextView) findViewById(R.id.match_phone);
        matchpriceView = (TextView) findViewById(R.id.match_price);
        matchinfoView = (TextView) findViewById(R.id.match_info);
        matchImageView = (ImageView) findViewById(R.id.match_image);
        callButton = (ImageButton) findViewById(R.id.call_Button);
        button_back = (ImageButton) findViewById(R.id.button_back);
        matchButton = (ImageButton) findViewById(R.id.matchButton);
        regButton = (Button) findViewById(R.id.matchreg_Button);
    }
     protected void initEvents() {
        matchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectMatch(String.valueOf(getUser_id()), String.valueOf(m_id),MatchDetailActivity.this);
            }
        });
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MatchModel matchModel = new MatchModel();
                matchModel.getIsNotReg(String.valueOf(getUser_id()), String.valueOf(m_id),MatchDetailActivity.this);
            }
        });
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });//返回
        callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + matchphoneView.getText().toString()));
                if (ActivityCompat.checkSelfPermission(MatchDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
    }
     protected void initData() {
        m_id = getIntent().getIntExtra("m_id", -1);
        MatchModel matchModel = new MatchModel();
        matchModel.matchDetail(String.valueOf(m_id), this);
        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollected(String.valueOf(getUser_id()), String.valueOf(m_id), "2", this);//flag=2.判断比赛关注状态
        MatchModel matchModels = new MatchModel();
        matchModels.getIsReg(String.valueOf(getUser_id()), String.valueOf(m_id), this);
    }
    @Override
    public void onSuccess(Object object,int flag)  {
        switch (flag){
            //flag标识为1，获取比赛详情
            case Const.MatchDetail:
                MatchBean matchDetail = (MatchBean) object;
                matchtitleView.setText(matchDetail.getM_name());
                matchaddressView.setText(matchDetail.getM_address());
                matchtimeView.setText(matchDetail.getM_time());
                matchphoneView.setText(matchDetail.getM_phone());
                matchpriceView.setText(String.valueOf(matchDetail.getM_price()));
                matchinfoView.setText("\u3000\u3000" + matchDetail.getM_info());
                final String matchPic = ConstPhoto.BASE_URL+ConstPhoto.MatchFile+ matchDetail.getM_picture();
                Picasso.with(context)
                        .load(matchPic)
                        .placeholder(R.drawable.pic2)
                        .into(matchImageView);
                break;
            case Const.IsAttention:
                //flag标识为2，判断关注状态
                IsAttentionBean isAttentionBean = (IsAttentionBean) object;
                if (isAttentionBean.getCollected().equals("1")) {
                    //已关注
                    flags=true;
                    matchButton.setImageResource(R.drawable.ic_attention_checked);
                }
                else {
                    //未关注
                    flags=false;
                    matchButton.setImageResource(R.drawable.ic_attention);
                }
                break;
            case Const.IsMatchReg:
                //flag标识为3，判断报名状态
                IsMatchReg isMatchReg=(IsMatchReg)object;
                if (String.valueOf(isMatchReg.getMn_paystate()).equals("1")){
                    //去支付
                    flagr=true;
                    regButton.setText("取消报名");

                }
                if (String.valueOf(isMatchReg.getMn_paystate()).equals("0")){
                    //已支付
                    regButton.setText("查看订单");
                    regButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.setClass(MatchDetailActivity.this,MatchOrderActivity.class);
                            intent.putExtra("m_id",m_id);
                            startActivityForResult(intent,1);
                        }
                    });
                }
                if (String.valueOf(isMatchReg.getMn_paystate()).equals("2")) {
                    //未报名
                    flagr=false;
                    regButton.setText("立即报名");
                    regButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent();
                            intent.setClass(MatchDetailActivity.this,MatchRegActivity.class);
                            intent.putExtra("m_id",m_id);
                            startActivityForResult(intent,1);
                        }
                    });
                }
                break;
            case Const.MatchAttention:
                AttentionBean attentionBean=(AttentionBean)object;
                if (attentionBean.getSuccess().equals("1")) {
                    Log.e(attentionBean.getSuccess(),"---------");
                    if(flags){
                        flags=false;
                        matchButton.setImageResource(R.drawable.ic_attention);
                    }else {
                        flags=true;
                        matchButton.setImageResource(R.drawable.ic_attention_checked);
                    }
                    Toast.makeText(MatchDetailActivity.this, "OK", Toast.LENGTH_SHORT).show();
                }
                break;
            case Const.NoReg:
                NoRegBean noRegBean = (NoRegBean)object;
                if (noRegBean.getSuccess().equals("1")) {
                    Log.e(noRegBean.getSuccess(),"---------");
                    if(flagr) {
                        flagr = true;
                        regButton.setText("立即报名");
                        regButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent();
                                intent.setClass(MatchDetailActivity.this,MatchRegActivity.class);
                                intent.putExtra("m_id",m_id);
                                startActivityForResult(intent,1);
                            }
                        });
                    }
                    Toast.makeText(MatchDetailActivity.this, "取消报名成功", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();//初始化
        initEvents();//点击事件处理
        initData();//

    }
}
